/**
 * 
 */
package com.hawkfly.interceptor;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wbcs.config.component.application.report.ReportBean;
import com.wbcs.system.ReportRequest;
import com.wbcs.system.component.application.report.configbean.editablereport.AbsEditableReportEditDataBean;
import com.wbcs.system.component.application.report.configbean.editablereport.EditableReportDeleteDataBean;
import com.wbcs.system.component.application.report.configbean.editablereport.EditableReportInsertDataBean;
import com.wbcs.system.component.application.report.configbean.editablereport.EditableReportUpdateDataBean;
import com.wbcs.system.intercept.AbsInterceptorDefaultAdapter;

/**
 * 登录系统后对系统数据进行增删改记录的日志拦截器
 * @author hawkfly
 */
public class InterceptorOperateLogForReport extends AbsInterceptorDefaultAdapter                    
{
    private static Log log=LogFactory.getLog(InterceptorOperateLogForReport.class);
    @Override
	public int doSave(ReportRequest rrequest, ReportBean rbean, AbsEditableReportEditDataBean editbean){
        //判断会话是否失效
        Object usercode = rrequest.getRequest().getSession().getAttribute("usercode");
        /*if(usercode == null)
            rrequest.getWResponse().getMessageCollector().error("会话失效，请重新登录！",true);*/
        //判断操作执行动作类型
        String reporttitle = rbean.getTitle(rrequest);
        String pagetitle = rbean.getPageBean().getTitle(rrequest);
        StringBuffer oprtype = new StringBuffer();
        oprtype.append(pagetitle);
        oprtype.append("页面");
        oprtype.append(reporttitle);
        oprtype.append("报表的");
        if(editbean instanceof EditableReportInsertDataBean){
            oprtype.append("保存动作..");
        }else if(editbean instanceof EditableReportUpdateDataBean){
            oprtype.append("更新动作..");
        }else if(editbean instanceof EditableReportDeleteDataBean){
            oprtype.append("删除动作..");
        }
        //执行日志插入动作
        Connection conn = rrequest.getConnection();
        PreparedStatement ps = null;
        String sql = "insert into CBSS_SYS_LOG(ID, MEM_CODE, OPR_TYPE, OPR_DATE, REMOTE_IP, REMARK) values(SEQ_ORGS.NEXTVAL, ?, ?, ?, ?, ?)";
        try
        {
           ps = conn.prepareStatement(sql);
           ps.setString(1,String.valueOf(usercode));
           ps.setString(2,oprtype.toString());
           ps.setDate(3,new java.sql.Date(new java.util.Date().getTime()));
           ps.setString(4,getIpAddr(rrequest.getRequest()));
           ps.setString(5,rrequest.getRequest().getSession().getId());
           ps.executeUpdate();
        }catch(SQLException e)
        {
            // TODO Auto-generated catch block
            log.error("执行日志插入动作出错！", e);
            rrequest.getWResponse().getMessageCollector().error("日志插入错误","日志错误",e);
            return WX_RETURNVAL_TERMINATE;
        }finally{
            try
            {
                ps.close();
                conn.close();
            }catch(SQLException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        return super.doSave(rrequest,rbean,editbean);
    }
    
    public String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        } if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        } if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}

